//
//  ViewController.m
//  14-bt
//
//  Created by GivenBmli on 16/8/2.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"
#import "imageCollectionViewCell.h"
#import "LableCollectionReusableView.h"
#import "LableAndCollectionReusableView.h"
#define kCollectionViewReture @"reture"
#define kCollectionReusableHeader  @"reuseableView"
#define kCoolectionViewHeaderNoCell @"header"
#define kRedomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1]
@interface ViewController ()<UICollectionViewDelegate , UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UICollectionViewFlowLayout *flow =[[UICollectionViewFlowLayout alloc]init];
    
    UICollectionView *colletiView = [[UICollectionView  alloc]initWithFrame:self.view.bounds collectionViewLayout:flow];
    CGFloat witdh = (CGRectGetWidth(self.view.bounds) - 10)/3;
//    item 大小
    flow.itemSize = CGSizeMake(witdh, witdh);
//    列间距
    flow.minimumLineSpacing = 5;
//    行间距
    flow.minimumInteritemSpacing = 5;
    flow.headerReferenceSize = CGSizeMake(0, 50);
    colletiView.delegate = self;
    colletiView.dataSource = self;
    colletiView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:colletiView];
  
    [colletiView registerClass:[imageCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReture];
    
    [colletiView registerClass:[LableCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionReusableHeader ];
//    第一种:通过上边距实现视图添加(UICollectionView本质是UIScorllView)
//    colletiView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, -295, self.view.bounds.size.width, 290)];
//    view.backgroundColor =[UIColor greenColor];
//    [colletiView addSubview:view];
//    第二种:自定义一个头视图
    [colletiView registerClass:[LableAndCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCoolectionViewHeaderNoCell];
    
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 400);
        
    }else {
        return CGSizeMake(0, 50);
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LableAndCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCoolectionViewHeaderNoCell forIndexPath:indexPath];
//        reusableView.backgroundColor = [UIColor redColor];
        return reusableView;
    }else{
        
        LableCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionReusableHeader forIndexPath:indexPath];
        //    reusableView.backgroundColor = [UIColor redColor];
        return reusableView;
        
    }
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }

       return 33;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    imageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReture forIndexPath:indexPath];
    cell.backgroundColor = kRedomColor;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
